    //
//  FileDetailViewController.m
//  studip
//
//  Created by anti on 05.07.10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "FileDetailViewController.h"


@implementation FileDetailViewController

@synthesize fileId = _fileId;

/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/

/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}
*/

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)popToLauncherView
{
	[self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)loadView
{
	[super loadView];
	
	[self.navigationItem setRightBarButtonItem:[[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"eingangsbildschirm"] style:UIBarButtonItemStyleBordered target:self action:@selector(popToLauncherView)] autorelease] animated:YES];
}

- (FileModel*) getFileFromId:(NSString*) fileId{

	CourseDummyData* dummy = [[CourseDummyData alloc] autorelease];
	NSMutableDictionary* dict = [dummy getDummyFilesById:@"1"];
	NSArray* keys = [dict allKeys];
	
	for(int i = 0; i < [keys count]; i++){
	
		NSArray* files = [dict objectForKey: [keys objectAtIndex:i]];
		
		for (int j = 0; j < [files count]; j++) {
			FileModel* file = (FileModel*) [files objectAtIndex:j];
			if ([file.fileId isEqualToString:fileId]) {
				return file;
			}
		}
	}
	
	return nil;
	
}

- (id)initWithFileId:(NSString*)fileId {
	// Note the [self init] here instead of [super init].
	if (self = [self init]) {
		self.tableViewStyle = UITableViewStyleGrouped;
		self.variableHeightRows = YES;
		self.fileId = fileId;
		
		//CourseModel* course = [[CourseModel alloc] initWithCourseId: courseId];
		FileModel* file = [self getFileFromId: fileId];
		NSMutableArray* items = [[NSMutableArray  alloc] init];
		NSMutableArray* sections = [[NSMutableArray  alloc] init];
		NSMutableArray* itemRows = [[NSMutableArray  alloc] init];
		
		self.title = file.name;
		
		[sections addObject:@"Details"];
		
		[itemRows addObject:[TTTableCaptionItem
							 itemWithText: file.name
							 caption: @"Name"]];
		
		[itemRows addObject:[TTTableCaptionItem
							 itemWithText: file.description
							 caption: @"Description"]];
		
		[itemRows addObject:[TTTableCaptionItem
							 itemWithText: file.size
							 caption: @"Size"]];
		
		[itemRows addObject:[TTTableCaptionItem
							 itemWithText: file.realName
							 caption: @"Filename"]];
		
		
		[items addObject: itemRows];
		
		TTSectionedDataSource* ds = [[TTSectionedDataSource alloc] autorelease];
		ds.sections = sections;
		ds.items = items;
		self.dataSource = ds;
		
		TT_RELEASE_SAFELY(sections);
		TT_RELEASE_SAFELY(items);
		
		
	}
	
	return self;
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end
